<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传测试</title>
    <script src="https://unpkg.com/vue@2.7.14/dist/vue.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="js/upload.js"></script>
</head>
<body>
<form action="image.do" method="post" enctype="multipart/form-data">
    <input name="file" type="file" accept="image/*">
    <button>上传</button>
</form>

<h1>文件上传组件</h1>
<div id="app">
    <upload url="image.do" @uploaded="imageUploaded">
        <img :src="imagePath" width="200px">
    </upload>

    <upload url="doc.do"  @uploaded="docUploaded">
        <button>上传文档</button>
    </upload>
    <a v-if="docPath" :href="docPath" >{{docPath}}</a>

   <h2>粘贴图片</h2>

    <upload url="image.do" @uploaded="imagePasted" disable-click="true">
        <div id="msg" contenteditable style="width: 300px; height: 200px; border: 1px #aaa solid"></div>
    </upload>

    <fieldset>
        <legend>测试结果</legend>
        <div id="res"></div>
    </fieldset>
</div>

<script>
    var v = new Vue({
        el : "#app",
        data : {
            imagePath: "https://img0.baidu.com/it/u=1821253856,3774998416&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
            docPath:""
        },
        created() {},
        methods : {
            imageUploaded(webPath){
                this.imagePath = "/"+webPath;
            },
            docUploaded(webPath){
                this.docPath = "/"+webPath;
            },
            imagePasted(webPath){
                let img = document.querySelector("#msg img[src^='data']");
                img.src = "/" + webPath;
                // 测试
                res.innerHTML = msg.innerHTML;
            }
        }
    })
</script>


</body>
</html>